<?

//include_once '/var/www/html/new/com.inc.php';
include("common/header.html");
include("common/left.html");
include("common/page.php");
include("common/billcommon.php");

$r=array();
$status=array();
//$memberID=$_SESSION['sid'];
$memberID=4;
$where="where aid=$memberID";

$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;
//若没选时间，默认是前一个月
$begintime=date('Y-m-01',strtotime("-1 month"));
$endtime=date('Y-m-d', strtotime("$begintime +1 month -1 day"));
$btime=isset($_REQUEST['btime'])?$_REQUEST['btime']:$begintime;
$where.=" and etime>='$btime 00:00:00'";
$etime=isset($_REQUEST['etime'])?$_REQUEST['etime']:$endtime;
$where.=" and etime<='$etime 23:59:59'";


if(isset($_REQUEST['status'])){
	$status=$_REQUEST['status'];
	$where.=" and status in (".implode(",",$status).")";
}

//echo $where;
//exit;
if($_REQUEST['etime']&&$_REQUEST['btime']){
	$str="select * from uu_bill $where limit $offset,$pageSize";
}else{
	$str="";
}
if($str!=""){
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$r[]=$row;
		$fid_arr[] = $row['fid'];
	}
	//查询分销商的account
	$sql="select id,account from pft_member where id IN(".implode(',', $fid_arr).")";
	$GLOBALS['le']->query($sql);
	$acc_fid = array();
	while($tmp_row = $GLOBALS['le']->fetch_assoc()) {
		$acc_fid[$tmp_row['id']] = $tmp_row['account'];
	}
	$a=microtime(true);
	//找出在账单期间结算的订单
    $orderArr = $ord = array();
    $fid_arr  = array();//购买人的ID
	foreach($r as $keys=>$row) {
		$r[$keys]['orders']=$r[$keys]['tnums']=$r[$keys]['money']="";
		$obj=$s->Order_Globle_Search("",$row['fid'],"","","","","","","","",
            $row['btime'],$row['etime'],"","","","1|6","0|1|2","",1,1,0,99999,
            "0","0","","0","","0","","",$row['aid'],0,"","","","");
		$or=simplexml_load_string($obj);
		foreach($or as $val){
			$orderArr[]=$val;
            //找出用授信支付的订单
            $sel=<<<SQL
  select id from pft_member_journal where fid={$row['fid']}
  and aid={$row['aid']} AND orderid='{$val->UUordernum}' and dtype=0
  and ptype=2 limit 1
SQL;
            $GLOBALS['le']->query($sel);
            if($GLOBALS['le']->fetch_assoc()){
					if((string)$val->UUaids==0){//直销订单
						$r[$keys]['orders']=$r[$keys]['orders']+1;
						$r[$keys]['tnums']=$r[$keys]['tnums']+(int)$val->UUtnum;
						$r[$keys]['money']=$r[$keys]['money']+(int)$val->UUtotalmoney;
					}else{//分销订单，处理分销价
						$tprice = $s->Dynamic_Price_And_Storage(
						$acc_fid[$r[$keys]['fid']],
						(int)$val->UUpid,
						substr((string)$val->UUordertime, 0, 10),
						1,0,0,$r[$keys]['aid'],'','');
						if($tprice){
							$tprice = @simplexml_load_string($tprice);
						}
						$r[$keys]['orders']+=1;
						$r[$keys]['tnums'] +=(int)$val->UUtnum;
						$r[$keys]['money'] +=(int)$val->UUtnum * (int)$tprice->Rec->UUsprice;
					}
            }
		}
        if (!$r[$keys]['money']) {
			unset($r[$keys]);
		}
	}
	echo '+++';
	echo microtime(true)-$a;
	//exit;
	
}
echo '+++';
echo microtime(true)-$a;
if($str==""){
	$r="";
}
$count=count($r);
$h3Title="应收款账单";
$about="get";

include("tpl/bill_dt.tpl");
include("common/footer.html");
?>